#ifndef __UART_H
#define __UART_H

#include "reg52.h"
#include "type.h"
#include "interrupt.h"     
#include "gpio.h"
#include "timer.h"
#include "stdio.h"
#include "lcd1602.h"
#include "memory.h"
#include "digital.h"

/***********************************************************************************************************************
�����ʶ���
***********************************************************************************************************************/
#define UART_BAUD_RATE_300    0x31
#define UART_BAUD_RATE_600    0x99
#define UART_BAUD_RATE_1200   0xCC
#define UART_BAUD_RATE_2400   0xE6
#define UART_BAUD_RATE_4800   0xF3
#define UART_BAUD_RATE_7200   0xF7

/***********************************************************************************************************************
У�鷽ʽ����
***********************************************************************************************************************/
#define PARITY_NONE  0
#define PARITY_ODD   1
#define PARITY_EVEN  2

#define PARITY_CHECK() ((~(GET_Bit(SCON, 2) ^ GET_Bit(PSW, 0)) && (PARITY_EVEN == uart_t.parity))||\
                          ( (GET_Bit(SCON, 2) ^ GET_Bit(PSW, 0)) && (PARITY_ODD  == uart_t.parity))||\
                          (PARITY_NONE == uart_t.parity)\
                                             )
                         

/***********************************************************************************************************************
���ڻ������ֽڳ���
***********************************************************************************************************************/
#define UART_BUFF_MAX 32

/***********************************************************************************************************************
��������
***********************************************************************************************************************/
#define SET_UART_8_BIT() do{SCON &= (~0x07 << 5);SCON |= (0x01 << 6);}while(0) 
#define SET_UART_9_BIT() do{SCON &= (~0x07 << 5);SCON |= (0x03 << 6);}while(0)

#define ENABLE_UART()    do{SCON |= (0x05 << 4);}while(0);
#define DISABLE_UART()   do{SCON &= ~(0x05 << 4);}while(0); 

#define UART_RATE_DOUBLE() do{SET_Bit(PCON, 7);}while(0)

extern INT8U uart_rx_buff_len;
extern INT8U uart_buffer[UART_BUFF_MAX];

/***********************************************************************************************************************
�������п���
***********************************************************************************************************************/
typedef struct _UART_T
{
    INT8U *tx_pointer;
        INT8U tx_buff_len;
        INT8U rx_buff_len;
        INT8U delay_ms;       
        INT8U rx_ep;
        INT8U parity:4,
              state:4;
        INT8U baud;
        INT8U baud_last;
}UART_T;

/***********************************************************************************************************************
����״̬����
***********************************************************************************************************************/
#define UART_CTL_ODR  SCON
#define UART_RX_MODE 0 
#define UART_TX_MODE 1
#define UART_RX_FLAG 0
#define UART_TX_FLAG 1

#define UART_RX_EN() do{uart_t.rx_ep = 0; uart_t.rx_buff_len = 0; uart_t.state = UART_RX_MODE;}while(0)
/***********************************************************************************************************************
���ں����ӿ�
***********************************************************************************************************************/
extern void uart_tx_data(INT8U *_data, INT8U lenth);
extern void uart_init(INT8U baud, INT8U parity );
extern void uart_data_process(void);
extern void AT_command_deal();
#endif